package com.aw.guiguzi.widgets;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.aw.baselib.util.AwRecyclerViewUtil;
import com.aw.baselib.util.AwScreenUtils;
import com.aw.guiguzi.R;
import com.aw.guiguzi.adapter.BettingReConfirmAdapter;
import com.aw.guiguzi.bean.NumItemBean;
import com.aw.guiguzi.util.DataUtil;

import java.util.List;

public class BettingReConfirmDialog {

    private Dialog dialog;
    private TextView tv_bettingReConfirmHint, tv_cancel, tv_confirm;
    private RecyclerView rcv_data;

    public Dialog getDialog() {
        return dialog;
    }

    public void showDialog(Activity activity, List<NumItemBean> selectList, final View.OnClickListener listener) {
        dismiss();
        dialog = new Dialog(activity, com.aw.baselib.R.style.prompt_dialog);
        dialog.setContentView(R.layout.view_dialog_betting_reconfirm);
        rcv_data = dialog.findViewById(R.id.rcv_data);
        tv_bettingReConfirmHint = dialog.findViewById(R.id.tv_bettingReConfirmHint);
        tv_cancel = dialog.findViewById(R.id.tv_cancel);
        tv_confirm = dialog.findViewById(R.id.tv_confirm);
        tv_bettingReConfirmHint.setText(DataUtil.INSTANCE.matcherSearchText(activity.getResources().getColor(com.aw.baselib.R.color.color_248EEE), "*注：含有中划线部分超出下注限制数量，无法翻倍\n如不做修改，直接点确认即可", "确认", false));
        BettingReConfirmAdapter adapter = new BettingReConfirmAdapter();
        AwRecyclerViewUtil.setRecyclerViewGridlayout(activity, rcv_data, adapter, 3);
        adapter.addAllData(selectList);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            NumItemBean itemBean = (NumItemBean) adapter1.getItem(position);
            if(view.getId() == R.id.cBox_status) {
                itemBean.setDouble(!itemBean.isDouble());
            }
            adapter1.notifyDataSetChanged();
        });
        tv_confirm.setOnClickListener(v -> {
            listener.onClick(v);
            dismiss();
        });
        tv_cancel.setOnClickListener(v -> {
            listener.onClick(v);
            dismiss();
        });
        if(AwScreenUtils.isLandscape(activity)) {
            setDialogSize(activity, 0.6, 0.6);
        } else {
            setDialogSize(activity, 0.9, 0.6);
        }
        dialog.show();
    }

    public void dismiss() {
        if(dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    /**
     * 自定义布局dialog
     */
    public void setDialogSize(Context context, double weight, double height) {
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager m = ((Activity) context).getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (d.getWidth() * weight); // 宽度设置为屏幕的weight
        p.height = (int) (d.getHeight() * height); // 宽度设置为屏幕的weight
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(p);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
    }
}
